package prometheusdeploylogic

import (
	"context"
	"errors"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"strings"
	"sync"
	"time"
	"yunzhan/common/models"
	utils "yunzhan/common/utils"
	"yunzhan/rpc-server/internal/svc"
	agent "yunzhan/rpc-server/pb"
)

type DeployPrometheusLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDeployPrometheusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeployPrometheusLogic {
	return &DeployPrometheusLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// DeployPrometheus 单机部署
func (l *DeployPrometheusLogic) DeployPrometheus(in *agent.DeployPrometheusRequest) (*agent.DeployResponse, error) {
	if in.NodeInfo == nil {
		return &agent.DeployResponse{
			Code:    500,
			Message: "未配置节点信息, 无法执行Prometheus部署",
		}, errors.New("未配置节点信息, 无法执行Prometheus部署")
	}

	resp := &agent.DeployResponse{
		Code:    200,
		Message: fmt.Sprintf("请求已接收，正在部署 %s 到节点 %s", in.ConfigInfo.ComponentName, in.NodeInfo.Host),
	}
	taskID := "agent_" + in.NodeInfo.Host

	go l.startDeployment(taskID, in)

	return resp, nil
}
func (l *DeployPrometheusLogic) startDeployment(taskID string, in *agent.DeployPrometheusRequest) {

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		defer wg.Done()

		// 创建 LogManager 实例
		logManager := models.NewLogManager(100, fmt.Sprintf("http://%s:%d", l.svcCtx.Config.RestServices["apiServer"].RestConf.Host, l.svcCtx.Config.RestServices["apiServer"].RestConf.Port))
		defer logManager.Close()

		//flushInterval := 5 * time.Second // 刷新间隔
		//maxBatchSize := 100              // 最大批量大小
		//retryDelay := 2 * time.Second    // 重试延迟
		//maxRetries := 3                  // 最大重试次数
		//
		//// 创建 ComponentInfoManager 实例
		//componentInfoManager := models.NewComponentInfoManager(
		//	1000,
		//	fmt.Sprintf("http://%s:%d", l.svcCtx.Config.RestServices["apiServer"].RestConf.Host, l.svcCtx.Config.RestServices["apiServer"].RestConf.Port),
		//	flushInterval,
		//	maxBatchSize,
		//	retryDelay,
		//	maxRetries)
		//defer componentInfoManager.Close()

		packageDirectory := in.ConfigInfo.PackagePath
		rtrimDir := utils.Rtrim(packageDirectory, "/")
		prometheusVersion := in.ConfigInfo.ComponentName

		installPath := utils.Rtrim(in.ConfigInfo.InstallPath, "/")

		baseInfo := fmt.Sprintf("安装包存放目录: %s, Prometheus version: %s, Prometheus 安装目录: %s", rtrimDir, prometheusVersion, installPath)
		step := 1
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", baseInfo, "Prometheus", "Server")

		checkPathCommand := fmt.Sprintf(`
if [ ! -d "%s" ]; then
   sudo mkdir -p "%s"
fi
`, installPath, installPath)

		step++
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "执行组件安装路径检查...", "Prometheus", "Server")
		output, err := utils.ExecCommand(checkPathCommand)
		if err != nil {
			message := fmt.Sprintf("检查组件安装路径异常: %v, output: %s", err, output)
			logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "failure", message, "Prometheus", "Server")
			return
		}
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "检查组件安装路径完成", "Prometheus", "Server")

		step++
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "解压安装包...", "Prometheus", "Server")
		tarCommand := fmt.Sprintf("sudo tar -xzf %s/%s -C %s", rtrimDir, prometheusVersion, installPath)
		output, err = utils.ExecCommand(tarCommand)
		if err != nil {
			message := fmt.Sprintf("解压安装包异常: %v, output: %s", err, output)
			logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "failure", message, "Prometheus", "Server")
			return
		}
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "解压安装包完成", "Prometheus", "Server")

		prometheusPackageName := utils.Rtrim(prometheusVersion, ".tar.gz")
		prometheusHome := fmt.Sprintf("%s/%s", installPath, prometheusPackageName)

		step++
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "修改组件所属用户组...", "Prometheus", "Server")
		chownCommand := fmt.Sprintf("sudo chown -R %s:%s %s", in.NodeInfo.Username, in.NodeInfo.Username, prometheusHome)
		output, err = utils.ExecCommand(chownCommand)
		if err != nil {
			message := fmt.Sprintf("修改组件所属用户组异常: %v, output: %s", err, output)
			logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "failure", message, "Prometheus", "Server")
			return
		}
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "修改组件所属用户组完成", "Prometheus", "Server")

		checkPathCommand = fmt.Sprintf(`
if [ ! -d "%s" ]; then
   sudo mkdir -p "%s"
fi
sudo chown -R %s:%s %s
`, in.PrometheusConfig.StorageTSdbPath, in.PrometheusConfig.StorageTSdbPath, in.NodeInfo.Username, in.NodeInfo.Username, in.PrometheusConfig.StorageTSdbPath)
		step++
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "检查组件数据存储目录...", "Prometheus", "Server")
		output, err = utils.ExecCommand(checkPathCommand)
		if err != nil {
			message := fmt.Sprintf("检查组件数据存储目录异常: %v, output: %s", err, output)
			logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "failure", message, "Prometheus", "Server")
			return
		}
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "检查组件数据存储目录完成", "Prometheus", "Server")

		prometheusServiceFile := fmt.Sprintf(`[Unit]
Description=Prometheus Monitoring Service
After=network.target

[Service]
User=%s
Group=%s
Type=simple
ExecStart=%s/prometheus \
  --config.file=%s/prometheus.yml \
  --storage.tsdb.path=%s \
  --web.max-connections=%s \
  --storage.tsdb.retention.time=%s \
  --web.listen-address=0.0.0.0:%s \
  --query.timeout=2m \
  --web.enable-lifecycle \
  --log.level=info

Restart=on-failure
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target`,
			in.NodeInfo.Username,
			in.NodeInfo.Username,
			prometheusHome,
			prometheusHome,
			in.PrometheusConfig.StorageTSdbPath,
			in.PrometheusConfig.WebMaxConnections,
			in.PrometheusConfig.StorageTSdbRetention,
			in.PrometheusConfig.WebListenAddress)
		step++
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "创建prometheus.service文件...", "Prometheus", "Server")
		writeServiceFileCommand := fmt.Sprintf("echo '%s' | sudo tee /etc/systemd/system/prometheus.service", prometheusServiceFile)
		output, err = utils.ExecCommand(writeServiceFileCommand)
		if err != nil {
			message := fmt.Sprintf("创建 prometheus.service 文件异常: %v, output: %s", err, output)
			logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "failure", message, "Prometheus", "Server")
			return
		}
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "创建prometheus.service文件完成", "Prometheus", "Server")
		step++
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "启动Prometheus服务...", "Prometheus", "Server")
		reloadSystemdCommand := fmt.Sprintf("sudo systemctl daemon-reload && sudo systemctl enable prometheus.service --now")
		output, err = utils.ExecCommand(reloadSystemdCommand)
		if err != nil {
			message := fmt.Sprintf("启动Prometheus服务异常: %v, output: %s", err, output)
			logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "failure", message, "Prometheus", "Server")
			return
		}
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "启动Prometheus服务完成", "Prometheus", "Server")

		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "等待Prometheus服务完全启动", "Prometheus", "Server")

		time.Sleep(3 * time.Second)
		step++
		statusCommand := "sudo systemctl is-active prometheus.service"
		output, err = utils.ExecCommand(statusCommand)
		if err != nil || strings.TrimSpace(output) != "active" {
			message := fmt.Sprintf("Prometheus启动后未正常运行: %v, output: %s", err, output)
			logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "failure", message, "Prometheus", "Server")
			return
		}
		logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "INFO", "Prometheus部署成功", "Prometheus", "Server")

		currentIP, currentIPErr := utils.GetCurrentInternalIP()
		if err != nil {
			message := fmt.Sprintf("%s: 无法获取当前主机IP: %v", in.NodeInfo.Host, currentIPErr)
			logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "failure", message, "Prometheus", "Server")
			return
		}
		currentHost, currentHostErr := utils.GetCurrentHostname()
		if err != nil {
			message := fmt.Sprintf("%s: 无法获取当前主机名: %v", in.NodeInfo.Host, currentHostErr)
			logManager.SendLog(in.ClusterID, in.NodeInfo.Host, step, "failure", message, "Prometheus", "Server")
			return
		}

		prometheusInfo := &models.ComponentInfo{
			ClusterID:      in.ClusterID,
			ComponentName:  "Prometheus",
			Version:        in.Version,
			NodeHost:       currentHost,
			NodeIP:         currentIP,
			ComponentRole:  "Server",
			HomePath:       prometheusHome,
			DataStorageDir: in.PrometheusConfig.StorageTSdbPath,
			Port:           in.PrometheusConfig.WebListenAddress,
			Status:         true,
			AdditionalInfo: fmt.Sprintf("config_file=%s/prometheus.yml", prometheusHome),
		}

		l.svcCtx.ComponentInfoManager.SendComponentInfo(prometheusInfo)

	}()
	wg.Wait()

}
